package com.bocek.chakmapoint.view.sort;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JButton;

import com.bocek.chakmapoint.controller.MainController;
import com.bocek.chakmapoint.controller.SlideEditController;
import com.bocek.chakmapoint.controller.SlideSortController;
import com.bocek.chakmapoint.view.MainView;
import com.bocek.chakmapoint.view.menubar.MenubarView;
import com.bocek.chakmapoint.view.slide.SlideView;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class SlideSortView extends JFrame {
	private static final long serialVersionUID = 3341769737191568679L;
	private static final SlideSortView instance = new SlideSortView();
	
	private int selected = -1;
	private JLabel lblNo;
	private JPanel centerPanel;
	
	public static SlideSortView getInstance() {
		return instance;
	}

	/**
	 * Create the frame.
	 */
	private SlideSortView() {
		
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				MainController.getInstance().switchToEdit();
				SlideView.getInstance().repaint();
				MenubarView.getInstance().updateLabels();
				MainView.getInstance().setVisible(true);
			}
		});
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 640, 480);
		getContentPane().setLayout(new BorderLayout(0, 0));
		
		JPanel topPanel = new JPanel();
		getContentPane().add(topPanel, BorderLayout.NORTH);
		
		JLabel lblSelectedIndex = new JLabel("Selected Index:");
		topPanel.add(lblSelectedIndex);
		
		lblNo = new JLabel("N/A");
		topPanel.add(lblNo);
		
		JPanel bottomPanel = new JPanel();
		getContentPane().add(bottomPanel, BorderLayout.SOUTH);
		
		JButton btnMoveLeft = new JButton("Move Left");
		btnMoveLeft.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SlideSortController.getInstance().selectSlide(selected);
				SlideSortController.getInstance().moveLeftButtonPressed();
				
				if (selected > 1) {
					setSelectedIndex(selected - 1);
					updateList();
				}
			}
		});
		bottomPanel.add(btnMoveLeft);
		
		JButton btnDelete = new JButton("Delete");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SlideSortController.getInstance().selectSlide(selected);
				SlideSortController.getInstance().deleteButtonPressed();
				
				if (selected > 1)
					setSelectedIndex(selected - 1);
				else
					setSelectedIndex(selected + 1);
				
				updateList();
			}
		});
		bottomPanel.add(btnDelete);
		
		JButton btnMoveRight = new JButton("Move Right");
		btnMoveRight.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SlideSortController.getInstance().selectSlide(selected);
				SlideSortController.getInstance().moveRightButtonPressed();
				
				if (selected < SlideEditController.getInstance().getSlideCount()) {
					setSelectedIndex(selected + 1);
					updateList();
				}
			}
		});
		bottomPanel.add(btnMoveRight);
		
		centerPanel = new JPanel();
		getContentPane().add(centerPanel, BorderLayout.CENTER);
		centerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
	}
	
	public void setSelectedIndex(int index) {
		
		if (index < 1  || index > SlideEditController.getInstance().getSlideCount())
			index = 1;
		
		selected = index;
		
		if (selected < 1) {
			lblNo.setText("N/A");
		} else {
			lblNo.setText(Integer.toString(selected));
		}
	}
	
	public void updateList() {
		centerPanel.removeAll();
		
		for (int i = 0; i < SlideEditController.getInstance().getSlideCount(); i++) {
			SlideThumbnail thumbnail = new SlideThumbnail(centerPanel, SlideSortController.getInstance().getSlideName(i));
			centerPanel.add(thumbnail);
		}
		centerPanel.repaint();
	}
}
